<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="form" :inline="true" v-show="showSearch" label-width="68px">
      <el-select v-model="queryParams.projectType" filterable @change="getList" placeholder="检查" :filter-method="getDictDataList"  v-hasPermi="['dms:used:query']">
        <el-option
          v-for="item in dictDataList"
          :key="item.dictValue"
          :label="item.dictLabel"
          :value="parseInt(item.dictValue)">
        </el-option>
      </el-select>
      <el-button style="margin-left: 10px"
        type="primary"
        icon="el-icon-plus"
        size="mini"
        @click="handleAdd"
        v-hasPermi="['dms:used:add']"
      >添加常用项
      </el-button>

    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5"></el-col>
      <div class="top-right-btn">
        <el-tooltip class="item" effect="dark" content="刷新" placement="top">
          <el-button size="mini" circle icon="el-icon-refresh" @click="handleQuery"/>
        </el-tooltip>
        <el-tooltip class="item" effect="dark" :content="showSearch ? '隐藏搜索' : '显示搜索'" placement="top">
          <el-button size="mini" circle icon="el-icon-search" @click="showSearch=!showSearch"/>
        </el-tooltip>
      </div>
    </el-row>

    <el-table v-loading="loading" :data="usedList">
      <el-table-column label="id" v-if="undefined" align="center" prop="id"/>
      <el-table-column label="项目名称" align="center" prop="name"/>
      <el-table-column v-if="queryParams.projectType!=4" label="单价" align="center" prop="price"/>
      <el-table-column v-if="queryParams.projectType==4" label="ICD编码" align="center" prop="icd"/>
      <el-table-column label="项目编码" align="center" prop="code"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['dms:used:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改医生常用诊断（疾病）、检查检验(搁置)对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1800" append-to-body>
      <el-form :model="queryParam" ref="form" :inline="true" v-show="showSearch" label-width="40px">
        <el-row :gutter="10" class="mb8">
        <el-col :span="8">
          <el-input v-model="queryParam.name" placeholder="搜索项目名"/>
        </el-col>
          <el-col :span="5">
          <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          </el-col>
        </el-row>
      </el-form>
      <el-table v-loading="loading" :data="addUsedList" highlight-current-row>
        <el-table-column label="id" v-if="undefined" align="center" prop="id"/>
        <el-table-column label="项目名称" align="center" prop="name"/>
        <el-table-column v-if="this.queryParams.projectType!=4" label="单价" align="center" prop="price"/>
        <el-table-column v-if="this.queryParams.projectType==4" label="ICD编码" align="center" prop="icd"/>
        <el-table-column label="项目编码" align="center" prop="code"/>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-add"
              @click="handleCurrentChange(scope.row)"
              v-hasPermi="['dms:used:add']"
            >添加
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total1>0"
        :total="total1"
        :page.sync="queryParam.pageNum"
        :limit.sync="queryParam.pageSize"
        @pagination="addUsedList"
      />
    </el-dialog>
  </div>
</template>

<script>
  import {listUsed, dictDataList, delUsed, addUsed, updateUsed,allDataList} from "@/api/dms/used";

  export default {
    name: "Used",

    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        total1: 0,
        //数据字典
        dictDataList: [],
        // 医生常用诊断（疾病）、检查检验(搁置)表格数据
        usedList: [],
        addUsedList:[],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          projectType: 1,
          id:null,
          name:null
        },
        queryParam: {
          pageNum: 1,
          pageSize: 5,
          projectType: 1,
          id:null,
          name:null
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {}
      };
    },
    created() {
      this.getDictDataList();
      this.getList();
    },
    methods: {
      getDictDataList() {
        this.loading = true;
        dictDataList(this.queryParams).then(response => {
          this.dictDataList = response.data;
          this.loading = false;
        });
      },
      /** 查询医生常用诊断（疾病）、检查检验(搁置)列表 */
      getList() {
        this.loading = true;
        listUsed(this.queryParams).then(response => {
          this.usedList = response.rows;
          this.dataIds=this.usedList[0].id;
          console.log( this.dataIds)
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          projectId:null,
          projectName:null,
          price:null,
          projectCode:null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.handleAdd();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      /** 新增查询按钮操作 */
      handleAdd(row) {
        this.reset();
        this.open = true;
        this.title = "添加医生常用项";
        /*this.queryParams.id=row.projectId;*/
        this.queryParam.projectType=this.queryParams.projectType;
        allDataList(this.queryParam).then(response => {
            this.addUsedList = response.rows;
          this.total1 = response.total;
          this.loading = false;
        });
      },

      /** 添加按钮操作 */
      handleCurrentChange(row){
        const name =row.name;
        let bool=false;
        for (let i = 0; i <this.usedList.length ; i++) {
          if(this.usedList[i].id==row.id){
            bool=true;
            break;
          }
        }
       this.$confirm('确定要将"' +name+ '"添加到常用项吗?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function (){

          if(bool){
            alert("不能添加重复的数据");
            this.open=false;
            return addUsed(null);
          }
          return addUsed(row.id);
        }).then(() => {
          this.msgSuccess("添加成功");
          this.open=false;
          this.getList();
        }).catch(function () {
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const name = row.name;

        this.$confirm('是否确认删除项目名称为"' + name + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function () {
          return delUsed(row.id);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function () {
        });
      },

    }
  };
</script>
